const banModel = require('../db/banans')
let {resinfo} = require('../utils/utils')
module.exports = {
    //增加数据
    setImg:async(req,res)=>{
        // console.log(req.body.img);
        let {img,name} = req.body
        let res1 = await banModel.insertMany({
            imgSrc:img,
            imgName:name
        })
        if(res1.length){
            resinfo(res,0,'成功',{res1})
        }else{
            resinfo(res,1,'失败')
        }
    },
    //获取数据
    getImg:async(req,res)=>{
        let res1 = await banModel.find()
        if(res1.length){
            resinfo(res,0,'成功',res1)
        }else{
            resinfo(res,1,'失败')
        }
        
    },
    //删除数据
    delImg:async(req,res)=>{
        let{id} = req.body
        try {
            await banModel.deleteOne({_id:id})
            resinfo(res,0,'删除成功')
        } catch (error) {
            resinfo(res,1,'删除失败')
        }
    },
    //修改数据
    upImg:async(req,res)=>{
        let {id,img,name} = req.body
        let res1 = await banModel.find({_id:id})
        if(!res1.length){
            resinfo(res,0,'找不到对应数据')
            return
        }
       try {
            let res2 = await banModel.updateOne({
                _id:id
            },{$set:{
                imgSrc:img,
                imgName:name
            }})
       } catch (error) {
            resinfo(res,1,'修改失败')
            return
       }
       resinfo(res,0,'修改成功')
    }
}